using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using UWBGL_WinForms_Lib1;
using UWBGL_XNA_Lib13;

namespace XNA_SceneNode
{
	class DrawHandlerBig : UWB_XNAWindowHandler
	{
		private UWBGL_UIWindow mWindow;
		private DrawHandlerMinimap _minimap;
		private float mouseWorldX, mouseWorldY;

		UWB_PrimitiveCircle lightHalo = new UWB_PrimitiveCircle();



		public DrawHandlerBig()
		{
			mWindow = new UWBGL_UIWindow(true);

			Color innerColor = new Color(new Vector4(.5f, .5f, .5f, 0.1f)); // grey
			Color outterColor = new Color(new Vector4(0.01f, 0.01f, 0.01f, .99f)); // black
			lightHalo.center = new Vector3(10, 10, 0);
			lightHalo.radius = 37f;
			lightHalo.setFlatColor(outterColor);
			lightHalo.setShadingColor(innerColor);
			lightHalo.enableBlending(true);
		}

		public bool Initialize(Control PlaceHolder)
		{
			if (!mWindow.ReplaceFormControl(PlaceHolder.Handle))
				return false;

			if (!mWindow.AttachHandler(this))
				return false;

			return true;
		}

		public override void DrawGraphics()
		{
			m_WCWindow = _minimap.getRedboxBounds();

			BeginScene();

			loadW2NDCXform();

			Program.getModel().drawModel();

			lightHalo.center = Program.getModel().getSubmarine().Center;
			lightHalo.radius = (new Vector3(_minimap.getRedboxBounds().height(), _minimap.getRedboxBounds().width(), 0)).Length();
			lightHalo.draw(eLevelofDetail.lodHigh, Program.getModel().getDrawHelper());
			
			EndSceneAndShow();
		}

		public override void OnMouseButton(bool down, MouseEventArgs e)
		{
			int deviceX = 0, deviceY = 0;
			hardwareToDevice(e.X, e.Y, ref deviceX, ref deviceY);

			switch (e.Button)
			{
				case MouseButtons.Left:
					if (down)
					{
						computeWorldCoordinates(deviceX, deviceY); // world coordinate
						//Program.getModel().setTranslation(mouseWorldX, mouseWorldY);
						Program.getModel().setSubmarineVelocityDirection(mouseWorldX, mouseWorldY);
						Program.getModel().increaseSubmarineSpeed();

						ScrollWorld();
					}
					else
					{

					}
					break;
				case MouseButtons.Right:
					break;
				case MouseButtons.Middle:
					break;
			}
		}

		public override void OnMouseMove(MouseEventArgs e)
		{
			int deviceX = 0, deviceY = 0;
			hardwareToDevice(e.X, e.Y, ref deviceX, ref deviceY);

			switch (e.Button)
			{
				case MouseButtons.Left:
					computeWorldCoordinates(deviceX, deviceY); // world coordinate
					//Program.getModel().setTranslation(mouseWorldX, mouseWorldY);
					Program.getModel().setSubmarineVelocityDirection(mouseWorldX, mouseWorldY);
					Program.getModel().increaseSubmarineSpeed();

					ScrollWorld();
					break;
				case MouseButtons.Right:
					break;
				case MouseButtons.Middle:
					break;
			}
		}

		// sets mouseWorld X and Y to actual coordinates of world
		public void computeWorldCoordinates(int deviceX, int deviceY)
		{
			float zoomBoxHeight = _minimap.getRedboxBounds().height();
			float zoomBoxWidth = _minimap.getRedboxBounds().width();

			float windowWidth = (float)mWindow.Width;
			float windowHeight = (float)mWindow.Height;

			mouseWorldX = (((float)deviceX) - (windowWidth / 2)) * (zoomBoxWidth / windowWidth) + _minimap.getRedboxBounds().getCenter().X;
			mouseWorldY = (((float)deviceY) - (windowHeight / 2)) * (zoomBoxHeight / windowHeight) + _minimap.getRedboxBounds().getCenter().Y;
		}

		// tie to the mini-map state
		public void addReferenceofMinimap(ref DrawHandlerMinimap theMinimap)
		{
			_minimap = theMinimap;
		}

		public void ScrollWorld()
		{
			Vector3 org = _minimap.getRedboxBounds().getMin(); // origin of view window
			UWB_BoundingBox worldBound = Program.getModel().getWorldBound(); // bound of entire world, not view
			Vector3 pos = Program.getModel().getSubmarine().Center; // world coordinate of submarine
			UWB_BoundingBox theRedBox = _minimap.getRedboxBounds(); // bound of the view

			// check for x-direction scrolling
			if (Math.Abs(pos.X - worldBound.getCenter().X) <
					worldBound.width() * 0.6f)
			{
				// now scroll according to location of the ball (left/right)
				float xPos = (pos.X - org.X);
				float rightMove = xPos - (0.7f * theRedBox.width());
				float leftMove = xPos - (0.3f * theRedBox.width());
				if (rightMove > 0f)
					org.X += rightMove;
				else
					if (leftMove < 0f)
						org.X += leftMove;
			}

			// check for y-direction scrolling
			if (Math.Abs(pos.Y - worldBound.getCenter().Y) <
					worldBound.height() * 0.6f)
			{
				// now scroll up/down
				float yPos = (pos.Y - org.Y);
				float upMove = yPos - (0.7f * theRedBox.height());
				float downMove = yPos - (0.3f * theRedBox.height());

				if (upMove > 0f)
					org.Y += upMove;
				else
					if (downMove < 0f)
						org.Y += downMove;
			}

			Vector3 corner1 = new Vector3(org.X, org.Y, 0);
			Vector3 corner2 = new Vector3(org.X + theRedBox.width(), org.Y + theRedBox.height(), 0);
			theRedBox.setCorners(corner1, corner2);

			_minimap.setRedboxBounds(theRedBox);
		}
	}
}
